﻿using Simulation.Framework;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.EventSystems;

namespace Easy.Game2d
{

    public partial class UIDialoguePanel : PanelBase
    {

        #region override
        public override void Init(params object[] data)
        {
            InitComponent();
        }

        public override void Clear()
        {
            ClearComponent();
        }

        public override void Open()
        {
            gameObject.SetActive(true);
        }

        public override void Close()
        {
            //ClearBGClickEvent();
            gameObject.SetActive(false);
        }
        #endregion

        #region data
        private UnityAction _pointerClickAction;
        private UnityAction _pointerClickAction02;
        private UnityAction _pointerClickAction03;


        private int _clickValue = 0;
        private StoryDialogueDatas _storyDialogueDatas;
        #endregion

        #region btn
        private void OnStartButtonButtonClicked()
        {
            StartBtnEvent();
        }
        private void StartBtnEvent()
        {
            SingleBehavior<StepManager>.getInstance().PlayStep(StepName.stepMonster);
            Close();
        }
        #endregion

        #region  event
        public void AddBGClickEvent()
        {
            _clickValue = 0;
            _storyDialogueDatas = new StoryDialogueDatas();
            EventTrigger trigger = BGEventTrigger;
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerClick;
            entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); });
            trigger.triggers.Add(entry);
        }
        public void ClearBGClickEvent()
        {
            EventTrigger trigger = BGEventTrigger;
            EventTrigger.Entry entry = new EventTrigger.Entry();
            trigger.triggers.Clear();
        }

        #endregion

        #region logic
        private void OnPointerDownDelegate(PointerEventData data)
        {
            _pointerClickAction?.Invoke();
        }
        private void ClickStrValue01()
        {
            if (_clickValue == 3)
            {
                BGState();
                
                return;
            }

            if (_clickValue < _storyDialogueDatas._strArray.Length) ContentTextTextMeshProUGUI.text = "<color=#ffffff00>游戏</color>" + _storyDialogueDatas._strArray[_clickValue] + "\n";
            _clickValue += 1;
        }
        private void ClickStrValue02()
        {
            _pointerClickAction02?.Invoke();
            Close();
        }
        private void ClickStrValue03()
        {
            _pointerClickAction03?.Invoke();
            Close();

        }
        #endregion

        #region action
        public void DialogueStep01()
        {
            BGState(false);
            _pointerClickAction = ClickStrValue01;

            if (_clickValue < _storyDialogueDatas._strArray.Length) ContentTextTextMeshProUGUI.text = "<color=#ffffff00>游戏</color>" + _storyDialogueDatas._strArray[_clickValue] + "\n";
            _clickValue += 1;
        }
        public void DialogueStep02(UnityAction unityAction)
        {
            _pointerClickAction02 = unityAction;
            BGState(false);

            //_clickValue += 1;
             ContentTextTextMeshProUGUI.text = "<color=#ffffff00>游戏</color>你的实力已经足够了，现在去讨伐真正的魔物吧！" ;
            _pointerClickAction = ClickStrValue02;
        }
        public void DialogueStep03(UnityAction unityAction)
        {
            _pointerClickAction03 = unityAction;
            BGState(false);
            _clickValue += 1;
            ContentTextTextMeshProUGUI.text = "<color=#ffffff00>游戏</color>你证明了自己的实力，接下来就开始属于你的旅程吧！"  ;
            _pointerClickAction = ClickStrValue03;
        }
        private void BGState(bool isState = true)
        {
            ImagebgRectTransform.gameObject.SetActive(!isState);
            btnBGImageImage.gameObject.SetActive(isState);
            StartButtonButton.gameObject.SetActive(isState);
        }
        #endregion
    }
}
